home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-10-30 | 8.3 KB | 327 lines | [TEXT/MPS ] |
- // OurMain.cp
- // The derived application class.
- // The TApplication and TDocument folders have been left unchanged.
- // The application is tailored in the functions below.
-
- #include "OurMain.h"
- #include "OurMainShared.h"
-
- #include <Dialogs.h>
- #include <Memory.h>
- #include <Menus.h>
- #include <Resources.h>
-
- TOurApp* gOurApp; // Our global application object.
-
- // ---------------------------------------------------------------------
- // main
- // Application entry point.
- // It all starts here.
- int
- main()
- {
- gOurApp = new TOurApp;
- if(!gOurApp)
- {
- return 0;
- }
- gOurApp->InitOurApp();
- gOurApp->EventLoop();
- return 0;
- }
-
- // ---------------------------------------------------------------------
- // TOurApp::DoAbout
- // Display the "About…" box.
- void
- TOurApp::DoAbout()
- {
- DialogPtr pDlog;
- Str255 versString;
- short itemType;
- Handle hItem;
- Rect box;
- short itemHit;
- GrafPtr savePort;
- // Get dialog.
- pDlog = GetNewDialog(rAboutDlog, kDefaultStorage, (WindowPtr) kInFrontOfAll);
- // Set version informaton.
- GetDItem(pDlog, iAboutTitle, &itemType, &hItem, &box);
- this->GetVersion(versString);
- SetIText(hItem, versString);
- // Add default outline around OK button.
- ShowWindow(pDlog);
- GetDItem(pDlog, iAboutOk, &itemType, &hItem, &box);
- GetPort(&savePort);
- SetPort(pDlog);
- PenSize(3,3);
- InsetRect(&box, -4, -4);
- FrameRoundRect(&box, 16, 16);
- PenNormal();
- SetPort(savePort);
- // Wait for user action.
- ModalDialog(kNoFilterProc, &itemHit);
- // Clean up.
- DisposDialog(pDlog);
- HiliteMenu(0);
- }
-
- // ------------------------------------------------------------------------
- // TOurMain::DoMenuCommand
- // This is called when an item is chosen from the menu bar (after calling
- // MenuSelect or MenuKey). It does the right thing for each command.
- // 18-Mar-92 Remove file menu.
- void
- TOurApp::DoMenuCommand(short menuID, short menuItem) // override
- {
-
- Str255 daName;
- short daRefNum;
-
- switch (menuID)
- {
- case mApple:
- switch ( menuItem )
- {
- case iAbout:// bring up alert for About
- this->DoAbout();
- break;
- default: // all non-About items in this menu are DAs et al
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- HiliteMenu(0);
- break;
- } // switch
- break;
- case mFile:
- switch ( menuItem )
- {
- case iQuit:
- this->ExitLoop();
- break;
- default:
- break;
- } // switch
- break;
- default:
- break;
- } // switch
- } // DoMenuCommand
-
- // ------------------------------------------------------------------------
- // TOurApp::GetVersion
- // Get the current version of the application.
- // Get long version string from 'vers' resource #1
- //
- void
- TOurApp::GetVersion(Str255 versStr)
- {
- Handle hRes;
- char *pRes;
- short i;
-
- if (hRes = GetResource('vers', 1))
- {
- pRes = *hRes;
- pRes += 7 + pRes[6]; // long version pstring
- for(i=0; i<=pRes[0]; i++)
- versStr[i] = pRes[i]; // copy version pstring
- }
- }
-
- // ---------------------------------------------------------------------
- // TOurApp::InitOurApp
- // Do the things that our derived application class requires.
- void
- TOurApp::InitOurApp()
- {
- SysEnvRec envRec;
- (void) SysEnvirons(curSysEnvVers, &envRec);
- // System 7 is required
- if(envRec.systemVersion<0x0700)
- {
- this->BigBadError(kUserStrID, kErrNotSevenOh);
- }
- // Install menus.
- Handle menuBar = GetNewMBar(rMenuBar);
- SetMenuBar(menuBar); // Copy to current menu list.
- DisposHandle(menuBar); // Don't need it anymore.
- // Add DA names to Apple menu.
- AddResMenu(GetMHandle(mApple), 'DRVR');
- DrawMenuBar();
- // Create a document, window, and window object.
- this->fDocList->AddDoc((TDocument*) new TOurDoc(kWindResID));
-
- }
-
- // ------------------------------------------------------------------------
- // TOurDoc::TOurDoc
- // Document object constructor.
- // The base class creates the Window Manager window pointer.
- // We create the window object here.
- // The resID is used by the base class constructor.
- TOurDoc::TOurDoc(short resID) : TDocument(resID)
- {
- this->fWinObj = new TWin;
- }
-
- // ------------------------------------------------------------------------
- // TOurDoc::~TOurDoc
- // Document object destructor.
- // Delete the window object.
- TOurDoc::~TOurDoc()
- {
- delete this->fWinObj;
- }
-
- // ------------------------------------------------------------------------
- // TOurDoc::DoUpdate
- // Document object.
- // Entry for update event action.
- void
- TOurDoc::DoUpdate()
- {
- BeginUpdate(this->fDocWindow);
- this->fWinObj->Draw();
- EndUpdate(this->fDocWindow);
- }
-
- // ------------------------------------------------------------------------
- // TWin::TWin
- // Window object constructor.
- // Creates the window contents (TArt objects).
- TWin::TWin()
- {
- this->fArt1 = new TArt;
- this->fArt1->Init(kArt1ID, kArt1LocV, kArt1LocH);
- this->fArt2 = new TArt;
- this->fArt2->Init(kArt2ID, kArt2LocV, kArt2LocH);
- this->fArt3 = new TArt;
- this->fArt3->Init(kArt3ID, kArt3LocV, kArt3LocH);
- }
-
- // ------------------------------------------------------------------------
- // TWin::~TWin
- // Window object destructor.
- // Deletes the window contents (TArt objects).
- TWin::~TWin()
- {
- delete this->fArt1;
- delete this->fArt2;
- delete this->fArt3;
- }
-
- // ------------------------------------------------------------------------
- // TWin::Draw
- // Window object.
- // Within BeginUpdate/EndUpdate.
- void
- TWin::Draw()
- {
- // Setup and do DeviceLoop drawing.
- // Pass the Window object in userData.
- long userData=(long)this;
- DeviceLoopFlags flags=0;
- GrafPtr myPort;
- GetPort(&myPort);
- DeviceLoop(myPort->visRgn, TWin::DrawProc, userData, flags);
- };
-
- // ------------------------------------------------------------------------
- // TWin::DrawProc
- // Called by DeviceLoop.
- // A static function. Must be in a resident segment, locked and unpurgeable.
- // Because it's static, it cannot access object member variables directly.
- // We use the window object passed in userData to access its variables.
- #pragma segment Main
- pascal void
- TWin::DrawProc(short depth, short /*deviceFlags*/,
- GDHandle hTargetDevice,
- long userData)
- {
- // Get the window object from userData.
- TWin* theWinObject = (TWin*) userData;
- // Use depth of 1 if we have a computer without CQD.
- depth = (hTargetDevice==NULL)?1:depth;
- // Draw our objects.
- theWinObject->fArt1->Draw(depth);
- theWinObject->fArt2->Draw(depth);
- theWinObject->fArt3->Draw(depth);
- };
-
- // ------------------------------------------------------------------------
- // TArt::Draw
- // All art objects (PICT's) are drawn from here.
- // This is where we distinguish between B&W and color renderings
- // of TArt objects. The B&W rendering has a resource ID that
- // is kBWOffset larger than its color counterpart.
- // If the function can't find one version, it will try the other.
- void
- TArt::Draw(short depth)
- {
- // Don't draw empty art.
- if(this->fPictID==0)
- return;
- PicHandle hPict;
- if(depth<8)
- {
- // Use B&W PICT.
- hPict = (PicHandle) GetResource('PICT', this->fPictID+kBWOffset);
- if(!hPict)
- {
- // No B&W PICT, try color (may really be B&W).
- hPict = (PicHandle) GetResource('PICT', this->fPictID);
- }
- }
- else
- {
- // Use color PICT.
- hPict = (PicHandle) GetResource('PICT', this->fPictID);
- if(!hPict)
- {
- // No color PICT, try B&W.
- hPict = (PicHandle) GetResource('PICT', this->fPictID+kBWOffset);
- }
- }
- if(hPict)
- {
- // We could have used the fDrawRect private member variable,
- // but the article used the access function so we'll stick
- // with that.
- Rect theDrawRect;
- this->GetDrawRect(theDrawRect);
- HLock((Handle) hPict);
- DrawPicture(hPict, &theDrawRect);
- HUnlock((Handle) hPict);
- }
- };
-
- // ------------------------------------------------------------------------
- void
- TArt::GetDrawRect(Rect& theRect)
- {
- theRect = this->fDrawRect;
- }
-
- // ------------------------------------------------------------------------
- // TArt::Init
- // Initialize the art object.
- // Set it's PICT ID and drawing location.
- void
- TArt::Init(short resID, short drawLocV, short drawLocH)
- {
- this->fPictID = resID;
- SetRect(&this->fDrawRect, 0, 0, 0, 0);
- // Normalize PICT to desired drawing location.
- PicHandle hPict = (PicHandle) GetResource('PICT', this->fPictID);
- if(hPict)
- {
- // Get the original drawing rect.
- this->fDrawRect = (**hPict).picFrame;
- // Normalize rect to 0,0 in case the artist didn't.
- OffsetRect(&this->fDrawRect, -this->fDrawRect.left,
- -this->fDrawRect.top);
- // Move rect to loc.
- OffsetRect(&this->fDrawRect, drawLocH, drawLocV);
- }
- };